package cn.kk.pzhu.gpaweb.controller;

import cn.kk.pzhu.gpaweb.entity.Gpa;
import cn.kk.pzhu.gpaweb.entity.Student;
import cn.kk.pzhu.gpaweb.service.GpaService;
import cn.kk.pzhu.gpaweb.service.impl.MainServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author KK
 * @CreateTime 2022-08-29
 */
@Controller
@RequestMapping("")
public class Handler {

    @Autowired
    private GpaService gpaService;

    @Autowired
    private MainServiceImpl mainService;

    @RequestMapping("/")
    public String root() {
        return "index";
    }

    @PostMapping("/api/Login")
    public ModelAndView login(String userAccount, String userPassword, String loginMethod, String encoded) {
        String cookie = mainService.login(userAccount, userPassword, loginMethod, encoded);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("list");
        List<Student> go = null;
        try {
            List<String> params = new ArrayList<>();
            params.add("kksj=2022-2023-2");
            params.add("kksj=2022-2023-1");
            go = (List<Student>) mainService.go(params, cookie, false).get("students");
            String kksj = "2022-2023";
            //统计绩点
            double GPA = 0.0, wholeCredit = 0.0;
            for (Student student : go) {
                wholeCredit += Double.parseDouble(student.getCredit());
            }
            for (Student student : go) {
                GPA += (Double.parseDouble(student.getGrade_one()) >= 60 ? ((Double.parseDouble(student.getGrade_one()) - 50) / 10.0) : 0) * (Double.parseDouble(student.getCredit()) / wholeCredit);
            }
            DecimalFormat df = new DecimalFormat("#.00");
            String v = df.format(GPA);
            Gpa gpa = new Gpa(go.get(0).getStuId(), go.get(0).getName(), Double.parseDouble(v), "2022-2023", go.get(0).getClasses(), new Date());
            gpaService.saveOrUpdate(gpa, new LambdaQueryWrapper<>(Gpa.class).eq(Gpa::getStuId, gpa.getStuId()).eq(Gpa::getTime, gpa.getTime()));

            if (go.size() <= 0) {
                modelAndView.addObject("html", "请检查cookie！");
            } else {
                StringBuffer s = new StringBuffer();
                s.append("<html><table class=\"table table-striped\"><tr><th scope=\"col\">课程名称</th><th scope=\"col\">成绩</th><th scope=\"col\">学分</th><th scope=\"col\">绩点</th><th scope=\"col\">开课时间</th></tr>");
                for (Student student : go) {
                    GPA += (Double.parseDouble(student.getGrade_one()) >= 60 ? ((Double.parseDouble(student.getGrade_one()) - 50) / 10.0) : 0) * (Double.parseDouble(student.getCredit()) / wholeCredit);
                    s.append("<tr><td scope=\"row\">").append(student.getSubject()).append("</td>");
                    s.append("<td scope=\"row\">").append(student.getGrade_one()).append("</td>");
                    s.append("<td scope=\"row\">").append(student.getCredit()).append("</td>");
                    s.append("<td scope=\"row\">").append(Double.parseDouble(student.getGrade_one()) >= 60 ? ((Double.parseDouble(student.getGrade_one()) - 50) / 10.0) : 0).append("</td>");
                    s.append("<td scope=\"row\">").append(student.getTime()).append("</td></tr>");
                }
                s.append("</table></html>");
                modelAndView.addObject("html", s.toString() + "<br><br><h3>" + kksj + "平均绩点为：" + v + "</h3>");
                modelAndView.addObject("oldCookie", cookie);
            }
        } catch (NullPointerException | SQLException d) {
            modelAndView.setViewName("error");
            d.printStackTrace();
            return modelAndView;
        }
        return modelAndView;
    }

    @GetMapping("/api/co")
    public String co(String oldCookie, String kksj, Model model) throws SQLException {
        List<Student> go = null;
        List<String> params = new ArrayList<>();
        switch (kksj) {
            case "2019-2020":
                params.add("kksj=2019-2020-1");
                params.add("kksj=2019-2020-2");
                break;
            case "2020-2021":
                params.add("kksj=2020-2021-1");
                params.add("kksj=2020-2021-2");
                break;
            case "2021-2022":
                params.add("kksj=2021-2022-1");
                params.add("kksj=2021-2022-2");
                break;
            case "2022-2023":
                params.add("kksj=2022-2023-1");
                params.add("kksj=2022-2023-2");
                break;
            case "2023-2024":
                params.add("kksj=2023-2024-1");
                params.add("kksj=2023-2024-2");
                break;
            case "xuanxiu":
                params.add("kcxz=06");
                break;
            default:
                params.add("kksj=" + kksj);
                break;
        }
        Map<String, Object> map = kksj.equals("xuanxiu") ? mainService.go(params, oldCookie, true) : mainService.go(params, oldCookie, false);
        go = (List<Student>) map.get("students");
        if (go == null || go.size() <= 0) {
            model.addAttribute("html", "请检查cookie！");
        }
        double GPA = 0.0, wholeCredit = 0.0;
        if (kksj.equals("xuanxiu")) {
            for (Student student : go) {
                wholeCredit += Double.parseDouble(student.getCredit());
            }
            StringBuffer s = new StringBuffer();
            s.append("<html><table class=\"table table-striped\"><tr><th scope=\"col\">课程名称</th><th scope=\"col\">成绩</th><th scope=\"col\">学分</th><th scope=\"col\">绩点</th><th scope=\"col\">开课时间</th></tr>");
            for (Student student : go) {
                s.append("<tr><td scope=\"row\">").append(student.getSubject()).append("</td>");
                s.append("<td scope=\"row\">").append(student.getGrade_one()).append("</td>");
                s.append("<td scope=\"row\">").append(student.getCredit()).append("</td>");
                s.append("<td scope=\"row\">").append(Double.parseDouble(student.getGrade_one()) >= 60 ? ((Double.parseDouble(student.getGrade_one()) - 50) / 10.0) : 0).append("</td>");
                s.append("<td scope=\"row\">").append(student.getTime()).append("</td></tr>");
            }
            s.append("</table></html>");
            model.addAttribute("html", s.toString() + "<br><br><h3>全部选修共修学分：" + wholeCredit + "</h3>");
            model.addAttribute("oldCookie", oldCookie);
            return "list::visit";
        }
        for (Student student : go) {
            wholeCredit += Double.parseDouble(student.getCredit());
        }
        StringBuffer s = new StringBuffer();
        s.append("<html><table class=\"table table-striped\"><tr><th scope=\"col\">课程名称</th><th scope=\"col\">成绩</th><th scope=\"col\">学分</th><th scope=\"col\">绩点</th><th scope=\"col\">开课时间</th></tr>");
        for (Student student : go) {
            GPA += (Double.parseDouble(student.getGrade_one()) >= 60 ? ((Double.parseDouble(student.getGrade_one()) - 50) / 10.0) : 0) * (Double.parseDouble(student.getCredit()) / wholeCredit);
            s.append("<tr><td scope=\"row\">").append(student.getSubject()).append("</td>");
            s.append("<td scope=\"row\">").append(student.getGrade_one()).append("</td>");
            s.append("<td scope=\"row\">").append(student.getCredit()).append("</td>");
            s.append("<td scope=\"row\">").append(Double.parseDouble(student.getGrade_one()) >= 60 ? ((Double.parseDouble(student.getGrade_one()) - 50) / 10.0) : 0).append("</td>");
            s.append("<td scope=\"row\">").append(student.getTime()).append("</td></tr>");
        }
        DecimalFormat df = new DecimalFormat("#.00");
        String v = df.format(GPA);
        s.append("</table></html>");
        if (kksj.equals("")) {
            model.addAttribute("html", s.toString() + "<br><br><h3>全部平均绩点为：" + v + "</h3><br><h3>总修学分为：" + map.get("allCredit") + "</h3>");
        } else {
            model.addAttribute("html", s.toString() + "<br><br><h3>" + kksj + "平均绩点为：" + v + "</h3>");
        }
        model.addAttribute("oldCookie", oldCookie);
        Gpa gpa = new Gpa(go.get(0).getStuId(), go.get(0).getName(), Double.parseDouble(v), StringUtils.hasText(kksj) ? kksj : "全部学期", go.get(0).getClasses(), new Date());
        gpaService.saveOrUpdate(gpa, new LambdaQueryWrapper<>(Gpa.class).eq(Gpa::getStuId, gpa.getStuId()).eq(Gpa::getTime, gpa.getTime()));
        return "list::visit";
    }
}
